const {
  CloudFunction
} = require('../Class/Cloud.js')

class get_class_info {
  // 这里放数据
  constructor() {
    this.alphabet_A = []
    this.alphabet_a = []
  }

  // 这里写方法
  GetClass_index() {
    const ClassInfo = new CloudFunction('GetClass_data');
    const result = ClassInfo.callAsync().then(res => {
      const hanyuyan = res.result.hyy_res.data[0]
      const xinwen = res.result.xinwen_res.data[0]
      // const wangxin = res.result.wangxin_res.data[0]
      this.Delet_(hanyuyan)
      this.Delet_(xinwen)
      // this.Delet_(wangxin)
      const hyy_totalclass = this.GetTotalClass(hanyuyan)
      const xinwen_totalclass = this.GetTotalClass(xinwen)
      // const wangxin_totalclass = this.GetTotalClass(wangxin)
      const hyy_lesson = this.GetTotalLesson(hyy_totalclass)
      const xinwen_lesson = this.GetTotalLesson(xinwen_totalclass)
      // const wangxin_lesson = this.GetTotalLesson(wangxin_totalclass)
      const total_lesson = [...hyy_lesson,...xinwen_lesson]
      return{hyy_lesson,xinwen_lesson,total_lesson}
    }).catch(err => {
      console.log('数据请求失败', err)
    })
    return result
  }

  // 这里是提取数据库中专业方向的全部课程 供上面 GetClass_index() 使用
  GetTotalClass(arr){
    var class_arr = Object.values(arr)
    var mergedArr = class_arr.flatMap(function(subArray){
      return subArray;
    })
    return mergedArr;
  }

  // 这里封装的是通过双循环提取lesson里面的数据
  GetTotalLesson(array){
    var lesson_info = []
    for (let index = 0; index < array.length; index++) {
      var array_lesson = array[index].lesson
      for (let j = 0; j < array_lesson.length; j++) {
        lesson_info.push(array_lesson[j])
      }
    }
    return lesson_info;
  }


  GetOnlineClass(){
    const ClassOnline = new CloudFunction('GetClass_data');
    const result = ClassOnline.callAsync().then(res=>{
      // 这里获取云数据中的数据
      const hyy = res.result.hyy_res.data[0]
      const xinwen = res.result.xinwen_res.data[0]
      // 这里获取不同专业的不同方向名字
      const hyy_zhuanye = Object.keys(hyy).filter(item => !item.includes("_"))
      const xinwen_zhuanye = Object.keys(xinwen).filter(item => !item.includes("_"))
      // 这里将不同专业的不同方向名字进行存储
      const hyy_options = this.Get_options(hyy_zhuanye,0)
      const xinwen_options = this.Get_options(xinwen_zhuanye,1)
      // 这里获取不同专业的不同方向的课程名称并进行存储
      this.Delet_(hyy)
      this.Delet_(xinwen)
      const hyy_text = this.GetClassText(hyy)
      const xinwen_text = this.GetClassText(xinwen)
      return {hyy,xinwen,hyy_options,xinwen_options,hyy_text,xinwen_text}
    }).catch(err=>{
      console.log(err)
    })
    return result
  }

  // 此方法是用来删除云数据中带有“_”的字段
  Delet_(arr){
    for (const key in arr) {
      if (key.startsWith('_')) {
        delete arr[key]
      } 
    }
  }

  // 此方法是存储不同专业不同方向名称
  Get_options(arr,num){
    var options =[]
    switch(parseInt(num)){
      case 0:
        for (let index = 0; index < arr.length; index++) {
          options.push({text:arr[index],value:index,icon:index})
        }
        break;
      case 1:
        var alphabet_A = [];
        for (let i = 65; i <= 90; i++) {
          alphabet_A.push(String.fromCharCode(i))
        }
        this.alphabet_A=alphabet_A
        for (let index = 0; index < arr.length; index++) {
          options.push({text:arr[index],value:alphabet_A[index],icon:index})
        }
        break;
      case 2:
        var alphabet_a =[]
        for (let i = 97; i <= 122; i++) {
          alphabet_a.push(String.fromCharCode(i))
        }
        this.alphabet_a = alphabet_a
        break;
    }


    return options
  }

  // 此方法是用来获取课程章节名称的
  GetClassText(data){
    const textArray = [];
    for (const key in data) {
      if (data.hasOwnProperty(key)) {
        const itemArray = data[key];
        itemArray.forEach(item => {
          if (item.hasOwnProperty('text')) {
            textArray.push(item.text);
          }
        });
      }
    }
    return textArray;
  }

  Basetext_Getclass(tab_id,menu_id,class_result){
    const hyy = class_result.hyy
    const xinwen = class_result.xinwen;
    var class_info = [];
    var total_info = []
    switch(tab_id){
      case 0:
        var text = Object.keys(hyy)
        for (let index = 0; index < text.length; index++) {
          if(class_result.hyy_options[parseInt(menu_id)].text == text[index]){
            class_info.push(hyy[text[index]])
          }
        }
        total_info = this.GetClassInfo(class_info[0])
        break;
      case 1:
        var text = Object.keys(xinwen)
        var menuid = this.alphabet_A.indexOf(menu_id)
        for (let index = 0; index < text.length; index++) {
          if(class_result.xinwen_options[menuid].text == text[index]){
            class_info.push(xinwen[text[index]])
          }
        }
        total_info = this.GetClassInfo(class_info[0])
        break;
      case 2:
        break;
    }
    return total_info
  }

  // 这个方法是用来获取上面 Basetext_Getclass() 传下来的数据进行处理 然后再返回数据
  GetClassInfo(class_info){
      const total_class = []
      const class_text = class_info.map(function(classObj) {
        return {
          text: classObj.text,
          flag: false
        };
      })
      class_info.forEach(function(classObj) {
        classObj.lesson.forEach(function(lesson) {
          total_class.push({
            img: lesson.img, teacher: lesson.teacher,
            teacher_img: lesson.teacher_img, teacher_introduce: lesson.teacher_introduce,
            text: lesson.text, url: lesson.url,
            title: classObj.text,
            zhuanye : lesson.zhuanye
          })
        })
      })
      return { class_text, total_class }
  }
}

// 这里暴露方法名
module.exports = {
  get_class_info
}